<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath;
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------

$categories = $db->getListOfCategories();

if (count($categories) > 0) {

    echo <<<BBB
        <div class="list-group">
    BBB;

    foreach ($categories as $category) {

        $categoryId = $category["id"];
        $title = $category["title"];
        $sortNumber = $category["sortNumber"];

        // 取得分类下网址数量
        $numberOfLinks = $db->getNumberOfLinksInCategory($categoryId);
        // 取得分类下网址的总点击次数
        $totalTimesOfClick = $db->getTotalTimesOfClickOfCategory($categoryId);

        echo <<<BBB
            <a href="linksInCategory.php?categoryId={$categoryId}" 
               class="list-group-item list-group-item-action d-flex align-items-center">
               <span class="w-100 text-break">$title</span>
               <span class="flex-shrink-0">
                   <span class="badge rounded-pill text-bg-light">{$numberOfLinks}网址</span>
                   <span class="badge rounded-pill text-bg-light">{$totalTimesOfClick}点击</span>
                   <span data-id="{$categoryId}" 
                       data-title="{$title}" 
                       data-sortnumber="{$sortNumber}" 
                       class="badge text-bg-dark btnManageCategory">管理</span>              
               </span>

            </a>
        BBB;

    }

    echo <<<BBB
        </div>
    BBB;


} else {
    echo "<p class='fw-light text-muted'>暂无，请先创建。</p>";
}
